package com.lab.service.impl;

import com.lab.dao.OperatorDao;
import com.lab.pojo.Operator;
import com.lab.service.OperatorService;
import com.lab.util.Md5Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("operatorService")
public class OperatorServiceImp implements OperatorService {
    @Resource
    OperatorDao operatorDao;
    //查询所有数据
    public List<Operator> allOperators() {
        return operatorDao.allOperators();
    }
    //添加和修改
    public void saveOperator(Operator operator) {
        try {
            Operator o= operatorDao.findOperatorById(operator.getMid());
            if (o!=null){
                //修改
                operatorDao.updateOperator(operator);
            }else {
                //增加
                // Md5对密码加密
                operator.setPassword(Md5Util.encodeByMd5(operator.getPassword()));
                SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
                sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记
                Date date = new Date();
                operator.setCreate_time(sdf.format(date));
                operatorDao.addOperator(operator);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除
    public void deleteOperator(Integer[] mid) {
        operatorDao.deleteOperator(mid);
    }

    public Operator findOperatorById(Integer mid) {
        return operatorDao.findOperatorById(mid);
    }

    //登录
    public Operator findOperator(String username) {
        return operatorDao.findOperator(username);
    }
}
